package cc.fs.zhihuapp.ui.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.model.Theme;
import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;
import cc.fs.zhihuapp.ui.fragment.NewDetailFragment;
import cc.fs.zhihuapp.ui.view.SwipeBackLayout;

/**
 * Created by fostion on 2015/9/6.
 */
public class NewsActivity extends BaseActivity {

    NewDetailFragment fragment;
    String newsId;
    @InjectView(R.id.backLayout)
    SwipeBackLayout backLayout;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, NewsActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_news_data);
        ButterKnife.inject(this);

        newsId = this.getIntent().getStringExtra("id");

        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        backLayout.setDragCallBack(new SwipeBackLayout.DragCallBack() {
            @Override
            public void onDrag() {
                finish();
            }
        });
        setSupportActionBar(toolbar);
        setupFragment();
    }

    private void setupFragment() {
        //解决fragment bug
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragment = (NewDetailFragment) fragmentManager.findFragmentByTag(NewDetailFragment.class.getName());
        if (fragment == null || fragment.isDetached()) {
            fragment = (NewDetailFragment) Fragment.instantiate(this, NewDetailFragment.class.getName());
            fragmentTransaction.add(R.id.container, fragment, NewDetailFragment.class.getName());
        }
        fragmentTransaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int menuId = item.getItemId();
        if (menuId == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void handler(Message msg) {

    }

    public String getNewsId() {
        return newsId;
    }

    public Toolbar getToolbar() {
        return toolbar;
    }
}
